﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using SetPipe.DataDefine;

namespace SetPipe
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdShowOrHidePipe : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			List<FamilySymbol> list = new List<FamilySymbol>();
			Transaction transaction = new Transaction(document, "ShowOrHidePipeOccupied");
			transaction.Start();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			IList<Element> list2 = filteredElementCollector.OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType().ToElements();
			bool inputValue = false;
			foreach (Element element in list2)
			{
				if (TypeFileInfo.OccupiedType.Contains((element as FamilyInstance).Symbol.Family.Name))
				{
					Parameter parameter = (element as FamilyInstance).Symbol.GetParameters("是否可见")[0];
					if (parameter != null)
					{
						if (parameter.AsInteger() == 1)
						{
							list.Add((element as FamilyInstance).Symbol);
							inputValue = true;
						}
						else
						{
							list.Add((element as FamilyInstance).Symbol);
						}
					}
				}
				else if (TypeEquipmentInfo.OccupiedType.Contains((element as FamilyInstance).Symbol.Family.Name))
				{
					Parameter parameter = (element as FamilyInstance).Symbol.GetParameters("是否可见")[0];
					if (parameter != null)
					{
						if (parameter.AsInteger() == 1)
						{
							list.Add((element as FamilyInstance).Symbol);
							inputValue = true;
						}
						else
						{
							list.Add((element as FamilyInstance).Symbol);
						}
					}
				}
			}
			list = list.Distinct<FamilySymbol>().ToList<FamilySymbol>();
			this.setHide(inputValue, list);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void setHide(bool inputValue, List<FamilySymbol> lstUseFamilySymbol)
		{
			int num = 1;
			if (inputValue)
			{
				num = 0;
			}
			foreach (FamilySymbol familySymbol in lstUseFamilySymbol)
			{
				try
				{
					Parameter parameter = familySymbol.GetParameters("是否可见")[0];
					if (parameter != null)
					{
						parameter.Set(num);
					}
				}
				catch
				{
				}
			}
		}
	}
}
